//
//  MQPaymentviewViewController.m
//  meiquan
//
//  Created by hejingjin on 2017/11/4.
//  Copyright © 2017年 mq. All rights reserved.
//

#import "MQPaymentviewViewController.h"
#import "MQPaymentTableViewCell.h"
#import "MQPayBottomview.h"
#import "MQBaseUserInfoModel.h"
#import <AlipaySDK/AlipaySDK.h>
@interface MQPaymentviewViewController ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong)MQPayBottomview *paybottomview;
@property(nonatomic,strong)UITableView *paytableview;

@property(nonatomic,copy)NSString *CertificateString;

@end

@implementation MQPaymentviewViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title = @"收银台";
    
    [self setupMainInterface];
    
    
    [self getqianming];
    
}

-(void)getqianming{
    
    MQBaseUserInfoModel *userinfo = [MQBaseUserInfoModel shareInstant];
    [userinfo loadUserinfoFromSadeBox];
    
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    
    [param setValue:_model.ID forKey:@"orderId"];
    
    [param setValue:userinfo.userId forKey:@"userId"];
    
    
    [MQRequestTools POSTWithMethod:orderInfo_initAlipay params:param success:^(id json) {
        
        NSLog(@"%@",json);
        _CertificateString = [json valueForKey:@"data"];
        
    } failure:^(NSError *error) {
        
    }];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MQPaymentTableViewCell *cell = [MQPaymentTableViewCell cellWIthTableview:tableView];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *appScheme = @"xiaojudan";
    NSString *signedString = _CertificateString;
//        NSString *signedString = @"alipay_sdk=alipay-sdk-java-dynamicVersionNo&app_id=2017110209684806&biz_content=%7B%22body%22%3A%22%E6%88%91%E6%98%AF%E6%B5%8B%E8%AF%95%E6%95%B0%E6%8D%AE%22%2C%22out_trade_no%22%3A%22201521343541211%22%2C%22product_code%22%3A%22QUICK_MSECURITY_PAY%22%2C%22subject%22%3A%22meiquan01-01%22%2C%22timeout_express%22%3A%2230m%22%2C%22total_amount%22%3A%220.01%22%7D&charset=utf-8&format=json&method=alipay.trade.app.pay&notify_url=http%3A%2F%2F1720rj8517.iok.la%2Fapp%2Fpay%2FalipaySync&sign=ThBoehnkreRtc0Mw5xuwAcO8kZxDjz8rsPhTKBI7kcBxPEHh4BZjAwaLqNGm0TL%2FGG%2F3weAYfxh85TJ1Sd%2BC6KM79duZI1o%2FkxZ%2BNMEAfsTLFqtWGD8xrIqD1Ap4cxh%2FsQCoumRDfclT%2BwchYSkRQJHSLPrZUbf%2FIAws8YVdXKyXbevDKWRqBbhc8LulqCiEEhXimYBI5uXsqZ3T40tpWtqrKTu%2FS5Jypp%2Be0437pO5MgpS78Qliv7W7u2IQcR%2FuXUM0TDiiHrmOr8i9PyhQagOEXN6W8E5TAIR1ZtvWlWPDqxo%2FmMUOD73GT9usVdgvGj9Zvsy1xakohuF4w%2B9wXw%3D%3D&sign_type=RSA2&timestamp=2017-11-05+12%3A31%3A43&version=1.0";
    
    
    //将签名成功字符串格式化为订单字符串,请严格按照该格式
    NSString *orderString = nil;
    if (signedString != nil) {
        //        orderString = [NSString stringWithFormat:@"%@&sign=\"%@\"&sign_type=\"%@\"",
        //                       orderSpec, signedString, @"RSA"];
        
//        orderString = _CertificateString;
        orderString = signedString;
        

        
        [[AlipaySDK defaultService] payOrder:orderString fromScheme:appScheme callback:^(NSDictionary *resultDic){
            
            if ( [resultDic[@"resultStatus"] isEqualToString:@"9000"])
            {
                [self.navigationController popToRootViewControllerAnimated:YES];
            }
            
            //            AFLog(@"reslut = %@",resultDic);
        }];
    }
}

-(void)setupMainInterface{
    
    self.view.backgroundColor = BSTColorFromRGB(0xefefef);
 
    
    _paybottomview = [MQPayBottomview loadpaybottomview];
    
    _paybottomview.backgroundColor = [UIColor whiteColor];
    _paybottomview.bst_x = 0;
    _paybottomview.bst_width = BSTScreenW;
    _paybottomview.bst_y = BSTScreenH - 51;
    _paybottomview.bst_height = 51;
    _paybottomview.paycountLabel.text = [NSString stringWithFormat:@"¥ %@",_model.payTotal];
    [self.view addSubview:_paybottomview];
    
    [self.view addSubview:self.paytableview];
}
-(UITableView *)paytableview{
    
    
    if (!_paytableview) {
        
        _paytableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 74, BSTScreenW, BSTScreenH -10 - 51 - NAVHEIGHT) style:UITableViewStylePlain];
        _paytableview.delegate = self;
        _paytableview.dataSource = self;
        _paytableview.backgroundColor = BSTColorFromRGB(0xf5f5f5);
        _paytableview.showsVerticalScrollIndicator = NO;
        _paytableview.bounces = NO;
        UIView *footerview = [UIView new];
        _paytableview.separatorStyle = UITableViewCellSeparatorStyleNone;
        _paytableview.backgroundColor = BSTColorFromRGB(0xf5f5f5);
        _paytableview.tableFooterView = footerview;
        
    }
    return _paytableview;
    
    
}

@end
